package Admin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JOptionPane;

import GroupClass.ConnectRMI;
import Project_etow.EtowInterface;

public class InsertFloor extends javax.swing.JDialog {

	private String idtow;

    public String getIdtow() {
		return idtow;
	}

	public void setIdtow(String idtow) {
		this.idtow = idtow;
	}

	public InsertFloor(java.awt.Frame parent, boolean modal,String idtow1) {
        super(parent, modal);
        idtow = idtow1;
        initComponents();
    }
    
    private String checkvisable;
	
	public String getCheckvisable() {
		return checkvisable;
	}

	public void setCheckvisable(String checkvisable) {
		this.checkvisable = checkvisable;
	}
	
	ConnectRMI connectRMI = new ConnectRMI();
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
    	
        lblTownID = new javax.swing.JLabel();
        lblFloorName = new javax.swing.JLabel(); 
        txtFTownID = new javax.swing.JTextField();
        txtFFloorName = new javax.swing.JTextField();
        btnSubmit = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
       
        lblTownID.setText("TownID");
        
        lblFloorName.setText("FloorName");

        txtFTownID.setText(idtow);
        txtFTownID.setEnabled(false);
        btnSubmit.setText("Submit");
        btnSubmit.setActionCommand("Submit");
        btnSubmit.addActionListener(new ActionListener() {			
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					try {
					    Registry registry = LocateRegistry.getRegistry(connectRMI.host,connectRMI.port);
	                                        EtowInterface obj=(EtowInterface)registry.lookup(connectRMI.regis);
						
						int townID = Integer.parseInt(txtFTownID.getText());
						String townidcheck = txtFTownID.getText();
						String floorName = txtFFloorName.getText();
						String statusFloor = "0";
						
						if (floorName.length() == 0) {
							JOptionPane.showMessageDialog(null, "Name Floor Not Null");
						}
						else if(statusFloor.length() == 0){
							JOptionPane.showMessageDialog(null, "Status Not Null");
						}
						
						String result = obj.InsertFloor(townID,floorName, statusFloor);
						if (townidcheck.length() != 0 && floorName.length() != 0 && statusFloor.length() != 0){
							if (result.equals("Insert")) {
								JOptionPane.showMessageDialog(null, "Insert Successful");								
								setCheckvisable("close");
								setVisible(false);
							}
							else {
								JOptionPane.showMessageDialog(null, "Insert Failed");
							}
						}
						
						}catch (Exception ex) { 
						ex.printStackTrace();
						}				
					
					System.out.println(getIdtow());
					  
		}
	});
      
        btnReset.setText("Reset");
        btnReset.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				txtFFloorName.setText("");
				
			}
		});
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setCheckvisable("close");
				setVisible(false);
			}
		});
       
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTownID)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblFloorName, javax.swing.GroupLayout.Alignment.TRAILING)
                                ))
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtFTownID, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)                          
                            .addComponent(txtFFloorName)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(btnSubmit)
                        .addGap(30, 30, 30)
                        .addComponent(btnReset)
                        .addGap(27, 27, 27)
                        .addComponent(btnCancel)))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTownID)
                    .addComponent(txtFTownID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFloorName)
                    .addComponent(txtFFloorName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   
                    )
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSubmit)
                    .addComponent(btnReset)
                    .addComponent(btnCancel))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        
        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
        
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(InsertFloor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(InsertFloor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(InsertFloor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(InsertFloor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
            	InsertFloor dialog = new InsertFloor(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                    	
                        System.exit(0);
                    }
                });
                
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JLabel lblFloorName;
    private javax.swing.JLabel lblTownID;
    private javax.swing.JTextField txtFFloorName;
    private javax.swing.JTextField txtFTownID;
    // End of variables declaration
}
